package com.zpyr.common;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieManager {
	
	public final static String COOKIE_STR_LOGIN_EMAIL = "ZPYR_LOGIN_EMAIL_CKE";
	public final static String COOKIE_STR_LOGIN_IP = "ZPYR_LOGIN_IP_CKE";
	
	private Cookie[] cookies;
	private HttpServletResponse response;
	
	
	public CookieManager(HttpServletRequest request , HttpServletResponse response) {
		cookies = request.getCookies();
		this.response = response;
	}
	
	public String getLoginId() {
		Cookie cookie = null;
		for(int i = 0 ; i < cookies.length ; i++) {
			cookie = cookies[i];
			if( COOKIE_STR_LOGIN_EMAIL.equals(cookie.getName()) ) {
				return cookie.getValue();
			}
		}
		return "";
	}
	
	public void setLoginId(String value) {
		Cookie cookie = new Cookie(COOKIE_STR_LOGIN_EMAIL , value);
		cookie.setPath("/");
		cookie.setMaxAge(60 * 60 * 24 * 7 * 4); // 4주일
		response.addCookie(cookie);
	}
	
	public void removeLoginId() {
		Cookie cookie = null;
		for(int i = 0 ; i < cookies.length ; i++) {
			cookie = cookies[i];
			if( COOKIE_STR_LOGIN_EMAIL.equals(cookie.getName()) ) {
				cookie.setMaxAge(0);
				cookie.setPath("/");
				cookie.setValue("");
				response.addCookie(cookie);
			}
		}
	}
	
	public String getLoginIp() {
		Cookie cookie = null;
		for(int i = 0 ; i < cookies.length ; i++) {
			cookie = cookies[i];
			if( COOKIE_STR_LOGIN_IP.equals(cookie.getName()) ) {
				return cookie.getValue();
			}
		}
		return "";
	}
	
	public void setLoginIp(String value) {
		Cookie cookie = new Cookie(COOKIE_STR_LOGIN_IP , value);
		cookie.setPath("/");
		cookie.setMaxAge(60 * 60 * 24 * 7 * 4); // 4주일
		response.addCookie(cookie);
	}
	
	public void removeLoginIp() {
		Cookie cookie = null;
		for(int i = 0 ; i < cookies.length ; i++) {
			cookie = cookies[i];
			if( COOKIE_STR_LOGIN_IP.equals(cookie.getName()) ) {
				cookie.setMaxAge(0);
				cookie.setPath("/");
				cookie.setValue("");
				response.addCookie(cookie);
			}
		}
	}

}
